package com.hzhetun.example.service.serviceimpl;

import com.hzhetun.example.mapper.CrmCustomerArchivesMapper;
import com.hzhetun.example.mapper.CrmCustomerMapper;
import com.hzhetun.example.pojo.CrmCustomer;
import com.hzhetun.example.pojo.CrmCustomerArchives;
import com.hzhetun.example.service.CrmCustomerArchivesService;
import com.hzhetun.example.service.CrmCustomerService;
import com.hzhetun.example.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenbingkun
 * @date 2018-12-23
 */
@Service
public class CrmCustomerArchivesServiceImpl implements CrmCustomerArchivesService{
    @Autowired
    private CrmCustomerArchivesMapper crmCustomerArchivesMapper;

    /**
     * 档案列表查询方法
     *
     * @param crmCustomerArchives
     * @param page
     * @param limit
     * @return List<CrmCustomerArchives>
     */
    @Override
    public List<CrmCustomerArchives> findCrmCustomerArchivesList(CrmCustomerArchives crmCustomerArchives, int page, int limit) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerId",crmCustomerArchives.getCustomerId());
        map.put("date",crmCustomerArchives.getDate());
        map.put("commissionDate",crmCustomerArchives.getCommissionDate());
        map.put("offset",(page-1)*limit);
        map.put("rows",limit);
        return crmCustomerArchivesMapper.findCrmCustomerArchivesList(map);
    }

    /**
     * 记录总数查询方法
     *
     * @param crmCustomerArchives
     * @return Long
     */
    @Override
    public Long countCrmCustomerArchives(CrmCustomerArchives crmCustomerArchives) {
        Map<String,Object> map=new HashMap<>(16);
        map.put("customerId",crmCustomerArchives.getCustomerId());
        map.put("date",crmCustomerArchives.getDate());
        map.put("commissionDate",crmCustomerArchives.getCommissionDate());
        return crmCustomerArchivesMapper.count(map);
    }

    /**
     * 保存或者修改方法
     *
     * @param crmCustomerArchives 客户档案实体类
     * @param username            操作人
     */
    @Override
    public void saveOrUpdate(CrmCustomerArchives crmCustomerArchives, String username) {
        //新增
        if(crmCustomerArchives.getId()==null){
            crmCustomerArchives.setCreateTime(new Date());
            crmCustomerArchives.setUpdateTime(new Date());
            crmCustomerArchives.setCreateUser(username);
            crmCustomerArchives.setUpdateUser(username);
            crmCustomerArchivesMapper.insertSelective(crmCustomerArchives);
        }
        //修改
        else{
            crmCustomerArchives.setUpdateTime(new Date());
            crmCustomerArchives.setUpdateUser(username);
            crmCustomerArchivesMapper.updateByPrimaryKeySelective(crmCustomerArchives);
        }
    }

    /**
     * 根据主键查询方法
     *
     * @param id
     * @return CrmCustomerArchives
     */
    @Override
    public CrmCustomerArchives findOne(int id) {
        return crmCustomerArchivesMapper.selectByPrimaryKey(id);
    }

    /**
     * 删除方法
     *
     * @param id
     * @return int
     */
    @Override
    public int del(int id) {
        return crmCustomerArchivesMapper.deleteByPrimaryKey(id);
    }
}
